package dubbo;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.dubbo.rpc.service.GenericException;
import com.alibaba.dubbo.rpc.service.GenericService;

/**
 * 泛服务类
 *
 */
public class CustomGenericService implements GenericService{
	
	private String interfaceName;
	
	public CustomGenericService(){}

	public CustomGenericService(String interfaceName){this.interfaceName = interfaceName;}

	public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
    	System.out.println(" ======GenericService start=========================================");
		System.out.println(" service: "+interfaceName);
		System.out.println(" method="+method);
		System.out.println(" parameterTypes="+Arrays.toString(parameterTypes));
		System.out.println(" args="+Arrays.toString(args));
    	System.out.println(" ======GenericService end  =========================================");
    	String result = "{ \"service\": \""+interfaceName+"\","+
    			"\"method\":\""+method+"\","+
    			"\"parameterTypes\":\""+Arrays.toString(parameterTypes)+"\","+
    			"\"args\":\""+Arrays.toString(args)+"\""+
    			"}";
    	if(interfaceName.equals("test.TestInterface")){
			return result;
		}else if(interfaceName.equals("test.TestInterface2")){
			if(method.equals("factorial")){
				int n = (Integer) args[0];
				long m = 1;
				for (int i=1;i<=n;i++){
					m *= i;
				}
				return m;
			}
			if(method.equals("add")){
				int n1 = (Integer) args[0];
				int n2 = (Integer) args[1];
				return n1+n2;
			}
		}else{
			return result;
		}
		return result;
	}

}
